package com.jc.camerascan.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jc.camerascan.R;
import com.jc.camerascan.entity.event.WXLoginGetTokenEvent;
import com.jc.camerascan.utils.XToastUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xuexiang.xui.utils.StatusBarUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * @ProjectName: PhotoRecognition
 * @Package: com.jc.camerascan.wxapi
 * @ClassName: WXEntryActivity
 * @Description: java类作用描述
 * @Author: XuChi
 * @CreateDate: 2020/7/17 11:04
 * @Version: 1.0
 */
public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler, WxConstants {

    private static final String TAG = WXEntryActivity.class.getSimpleName();

    private IWXAPI mApi;

    private static final int RETURN_MSG_TYPE_LOGIN = 1; //登录


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty);
        StatusBarUtils.translucent(this);
        Log.e(TAG, "onCreate");
        mApi = WXAPIFactory.createWXAPI(this, WX_APP_ID);
        try {
            boolean result = mApi.handleIntent(getIntent(), this);
            if (!result) {
                Log.d(TAG, "参数不合法，未被SDK处理，退出");
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Override
//    protected int getLayoutId() {
//        return R.layout.activity_empty;
//    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.alpha = 1f;//1.０全透明．０不透明．
        window.setAttributes(windowParams);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mApi.handleIntent(data, this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mApi.handleIntent(intent, this);
        finish();
    }


    @Override
    public void onReq(BaseReq baseReq) {
        Log.e(TAG, "errCode=" + baseReq.toString());
    }

    @Override
    public void onResp(BaseResp baseResp) {
        int result = 0;
        int type = baseResp.getType();
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if (type == RETURN_MSG_TYPE_LOGIN) {
                    String code = ((SendAuth.Resp) baseResp).code;
                    EventBus.getDefault().post(new WXLoginGetTokenEvent(code));
                } else {

                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                XToastUtils.error("取消");
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                XToastUtils.error("拒绝");
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                XToastUtils.error("不支持");
                break;
            default:
                XToastUtils.error("返回");
                break;
        }
        finish();
    }
}



